﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RenameTool.Rules.CApSearchAndReplace
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Text.RegularExpressions;
using System.Windows;


namespace DnProcessSimulateCommands.RenameTool.Rules;

[Export(typeof (RuleBase))]
public class CApSearchAndReplace : RuleBase
{
  private object[] args;
  private string displayString = "";

  public CApSearchAndReplace() => this.ArgsLength = 6;

  public override RuleBase CreateInstance() => (RuleBase) new CApSearchAndReplace();

  public override int Position => 2;

  public override RuleMessage Execute(List<string> objects)
  {
    if (objects == null)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_NULL_REF_OBJ"));
    if (this.Args == null)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_NULL_REF_ARGS"));
    if (this.Args.Length != this.ArgsLength)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_ARG_COUNT"));
    if (!(this.Args[0] is bool) || !(this.Args[1] is string) || !(this.Args[2] is bool) || !(this.Args[3] is string) || !(this.Args[4] is bool) || !(this.Args[5] is bool))
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_ARG_TYPE"));
    bool flag1 = (bool) this.Args[0];
    string str = (string) this.Args[1];
    bool flag2 = (bool) this.Args[2];
    string replaceString = (string) this.Args[3];
    bool flag3 = (bool) this.Args[4];
    bool flag4 = (bool) this.Args[5];
    if (!flag1 && str.Length == 0)
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_EMTPY_SEARCH_STRING"));
    PartialStringParser partialStringParser = new PartialStringParser();
    RuleMessage ruleMessage;
    try
    {
      ruleMessage = partialStringParser.Parse(replaceString);
    }
    catch
    {
      return new RuleMessage(false, XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_ERROR_PARSING_FAILED"));
    }
    if (!ruleMessage.Success)
      return ruleMessage;
    List<Dictionary<string, string>> dictionaryList = new List<Dictionary<string, string>>();
    List<string> attributes = partialStringParser.GetAttributes();
    if (attributes != null && attributes.Count > 0)
    {
      List<Dictionary<string, string>> attributesValuesAsString = CApRenameToolManager.GetInstance().GetAttributesValuesAsString(attributes, out this.ruleState);
      partialStringParser.SetAttributesValues(attributesValuesAsString);
    }
    else
      this.ruleState = RuleState.Success;
    if (flag1)
    {
      for (int index = 0; index < objects.Count; ++index)
        objects[index] = partialStringParser.GetNextString(index);
    }
    else
    {
      RegexOptions options = flag2 ? RegexOptions.None : RegexOptions.IgnoreCase;
      string pattern = Regex.Escape(str);
      if (flag4)
        pattern = pattern.Replace("\\*", ".*").Replace("\\?", ".").Replace("\\.*", "\\*").Replace("\\.", "\\?").Replace("\\\\", "\\");
      if (flag3)
        pattern = $"^{pattern}$";
      Regex regex = new Regex(pattern, options);
      string nextString;
      for (int index = 0; index < objects.Count; ++index)
      {
        for (Match match = regex.Match(objects[index]); match.Success; match = regex.Match(objects[index], match.Index + nextString.Length))
        {
          nextString = partialStringParser.GetNextString(index);
          objects[index] = objects[index].Substring(0, match.Index) + nextString + objects[index].Substring(match.Index + match.Length);
        }
      }
    }
    return new RuleMessage(true);
  }

  public override object[] Args
  {
    get => this.args;
    set
    {
      if (value != null && value.Length == 4 && value[0] is bool && value[1] is string && value[2] is bool && value[3] is string)
      {
        this.args = new object[this.ArgsLength];
        for (int index = 0; index < 4; ++index)
          this.args[index] = value[index];
        this.args[4] = (object) false;
        this.args[5] = (object) false;
      }
      else
        this.args = value;
      this.UpdateDisplayString();
    }
  }

  public override string DisplayString => this.displayString;

  private void UpdateDisplayString()
  {
    try
    {
      this.displayString = "";
      if ((bool) this.Args[0])
      {
        if ((string) this.Args[3] == "")
          this.displayString = XamlResourceManager.GetString("RENAME_TOOL_DS_SR_REMOVE_NAME");
        else
          this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_SR_RENAME_WITH"), this.Args[3]);
      }
      else
      {
        this.displayString = string.Format(XamlResourceManager.GetString("RENAME_TOOL_DS_SR_SEARCH_REPLACE"), this.Args[1], this.Args[3]);
        if (!(bool) this.Args[2] && !(bool) this.Args[4] && !(bool) this.Args[5])
          return;
        this.displayString += " (";
        if ((bool) this.Args[2])
          this.displayString += XamlResourceManager.GetString("RENAME_TOOL_MATCH_CASE");
        if ((bool) this.Args[4])
        {
          if ((bool) this.Args[2])
            this.displayString += ", ";
          this.displayString += XamlResourceManager.GetString("RENAME_TOOL_MATCH_ENTIRE_NAME");
        }
        if ((bool) this.Args[5])
        {
          if ((bool) this.Args[2] || (bool) this.Args[4])
            this.displayString += ", ";
          this.displayString += XamlResourceManager.GetString("RENAME_TOOL_USE_WILDCARDS");
        }
        this.displayString += ")";
      }
    }
    catch
    {
    }
  }

  public override string Header
  {
    get => XamlResourceManager.GetString("RENAME_TOOL_SEARCH_AND_REPLACE_HEADER");
  }

  public override RuleWindowBase CreateWindow(Window parent)
  {
    return (RuleWindowBase) new CUiSearchAndReplace(parent);
  }
}
